﻿namespace IBTrader.Charts
{
    using IBTrader.Modules.Read;
    using System.Collections.Concurrent;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    abstract class BaseChart : ChartArgs, IChart
    {
        private Color color = Color.Black;
        private bool init;
        public virtual ConcurrentBag<Line> Lines { get; private set; }
        protected readonly static Font Font = new Font("Arial", 10, FontStyle.Bold);
        protected readonly Worker Worker;
        protected float Width = 1;
        protected readonly int Alpha = 255;
        protected readonly DashStyle DashStyle = DashStyle.Solid;
        protected BaseChart(Worker worker)
        {
            if (worker != null) worker.Filters.Add(this);
            this.Worker = worker;
            Lines = new ConcurrentBag<Line>();
        }
        public virtual void Add(object sender, Line line)
        {
            if (!init)
            {
                if (sender is IChartArgs)
                    InitArgs(sender as IChartArgs);
                init = true;
            }
            Lines.Add(line);
        }
        public Color Color
        {
            get { return Color.FromArgb(Alpha, color); }
            set { color = value; }
        }
        protected SolidBrush Brush(Color? color = null) { return new SolidBrush(color ?? Color); }
        protected Pen Pen(Color? color = null) { return new Pen(color ?? Color, Width * (float)BaseGraphics.Factor) { DashStyle = DashStyle }; }
        public abstract void Draw(IGraphics g, Region region);
        public virtual void Clear()
        {
            Lines = new ConcurrentBag<Line>();
            init = false;
        }
    }
}
